package com.example.demo.main;

import cn.hutool.json.JSONUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicSessionCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.tencent.cloud.CosStsClient;
import com.tencent.cloud.Response;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.TreeMap;

@Slf4j
public class TencentTemporaryKey {

    public static void main(String[] args) {
        TreeMap<String, Object> config = new TreeMap<String, Object>();

        try {
            // 云 api 密钥 SecretId
            config.put("secretId", "AKID0xnXNBd1JVWN9uU9iDYhjgjfOG1ZzbIn");
            // 云 api 密钥 SecretKey
            config.put("secretKey", "UnwPjx32ucIrKlRFxXzZ1a3Mcn5EA0fB");

            // 临时密钥有效时长，单位是秒
            config.put("durationSeconds", 1800);

            // 换成你的 bucket
//            config.put("bucket", "q4-record-1300314960");
            config.put("bucket", "qukan-test-1300314960");

            // 换成 bucket 所在地区
            config.put("region", "ap-beijing");

            // 可以通过 allowPrefixes 指定前缀数组, 例子： a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
            config.put("allowPrefixes", new String[]{
                    "*"
            });

            // 密钥的权限列表。简单上传和分片需要以下的权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
            String[] allowActions = new String[]{
                    // 简单上传
                    "name/cos:PutObject",
                    "name/cos:PostObject",
                    // 分片上传
                    "name/cos:InitiateMultipartUpload",
                    "name/cos:ListMultipartUploads",
                    "name/cos:ListParts",
                    "name/cos:UploadPart",
                    "name/cos:CompleteMultipartUpload"
            };
            config.put("allowActions", allowActions);
            Response response = CosStsClient.getCredential(config);
            System.out.println("==============================================================1");
            System.out.println(JSONUtil.toJsonPrettyStr(response));
            System.out.println("==============================================================2");
            //上传
            extracted(response);


        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("no valid secret !");
        }
    }

    private static void extracted(Response response) {
        // 1 传入获取到的临时密钥 (tmpSecretId, tmpSecretKey, sessionToken)
        String tmpSecretId = response.credentials.tmpSecretId;
        String tmpSecretKey = response.credentials.tmpSecretKey;
        String sessionToken = response.credentials.sessionToken;

//        tmpSecretId = "AKIDwapfNBzPfrltxCFxIu2CIMMEyy5ZmI9v1aDYNo7kJAb-EiVOWao4SbJwZ65xuvjK";
//        tmpSecretKey = "HZE2GY1vltZ/9tHdWHoJ4+csCsiz3RTEaEyY1GMvC4M=";
//        sessionToken = "T2fWV12SVT1iVQbX95IsG4hEq9JbBsiadf65b06ee6e207966ae56ef8659d4984VPYnGRJPmdVhrn_jfW7iMWcskXekoJcjnlF3W4vXMM4Na3EQi0_3aKbMco_JF0wDr9GAu-xIrFlIWxo11TAcpV-tN7JWE5P23BcsdLtDOMM9yA0xxy_9Qn5_rKd7yU6f5YNZfsYO5SrsHamhyPm_rH29NeT02GR1uAHDRaYz25uoYTe8iIJvXdhHkuHdRMkaJRMFCo4eZrV_ZyUo38No8QQghN3DPgl7fEplEqs4Kg4lletXlM50Lu4oEdxd-jZbCh6DxIYGe9aN6zt7Zz-bSWC452bfO2fcpaxFnH_AgfK4mjNPPZCRMSnBdmLCdf-7gpSG3cMhTRxGTA3xw3dW0K-7RJp16pyWugGVMhT476uZirAOe3MlnDbXtI6HN_0cBs-OEM0jkUrJXv1OSyAAQZJL-4SNyIlX1_lnNbi48LaUTy7ldYW7AJri5kz-DdizRVHktS5PIUImU1gHjXzP6J307k7Zt9aJgVMqWFviV2OIL-2cqmpcv5EFSHnFVKLCW8rx72N19i-Xy4PQqPV_Eyx32GrZ3XBJeo4vAF4DwdD0n0WZrqXVMfXBE4n1-A4O";
         // Niuxi
        tmpSecretId = "AKIDvLeHSH6sUiCvYhTNR2UqUWPleNS-n2XU6OK3m77cL4YQE8mmPXnvUouoX-GHdZMw";
        tmpSecretKey = "'Hx+wAOi7/WDk0tV/1yH7ho3w26tvd1i5JXzgc9bqfYs=";
        sessionToken = "hvdwabaKEsAn6Jykn6cMIi4QwO1CiqPa40e569352bc0229c7c1e611c170a2f38S3oKCpwkuZNu6d3zOYbDKER2n9YNZXYsaY_PrBncI0t1rm1Cq1STEsneEGypDQfk6puT2AXaWDQGzY8nqBQLCS83ZeD_T_R6VhDqzpq5Pg_D8MjPCCO15LKXZLD47gXo3n-Mwy_CGd4fPOW1l25pXAheCKwZ9W5Es8J0PCrB8fZsbcRpouRrIHuJRrbLs63EyjyoA5NRue-HSuqjBo061JWufwaazqo3RE3AUgIbBgTWL6KQWBJ5ezagGxhkETaxiMNhjErXo8yDfOTgSDw85gLzODa9cdJRRS6_ZGeZg4LD01ehHEC9ES5bgpvu_Ugdy9z0Alr4bTk1G-DgfYCCgwAeV_2B1OTDDFV_HjcDdti98vxmEYBioC6QhwNuFnCix5FYbeU8ULeMshCAvmOLPvrfBtdyQ2oMgVfuNr3mvtIElBllKXC6InL7cFF5cJNe4DSL1D8iaSC82nwCO1wG-lg81kghIPNWnVXjDd3FevEpVRhyFwiXx1KDh0O2ny-faOqGpOHN8KpBSigeeJbBluLcf19mQF7WnbnDT-2R-5BF1c5cV0rz5a8-mcqY2uP1";
//        tmpSecretId = "AKIDj1cw4mHV8_0b-NNftj_iD-UNEd0N9jCzPYlOY4yG9FZ-hXzma4OIH-sK8x7M9lAh";
//        tmpSecretKey = "MRxKpTX4n55yW3oKQR5IKdGWkHG6NxQaeztmbivxyek=";
//        sessionToken = "hvdwabaKEsAn6Jykn6cMIi4QwO1CiqPaf68ac99e3279a26bef3060a5530fde60S3oKCpwkuZNu6d3zOYbDKCaRdtchszWduxvnmBCBleUWWU0r9waBrE-oJZJBBj7UYJpbCt0NST7kZt2sihCHjRWBbSV0BWzPWdxBStUmPRZQvUyP2kfkjQ37sdR5DIhXoHVVzW4bjT9QfGf6wuPG2lVBrUS-EG5NNx36jAPhukttAdvrNy6airhWykYWOIq_NLLzZg5GzU6E6tubrW0LnjnamGr9sWh03SFqlDF2MhdxH6Mw6pJD-uSQaSi5LP8Lq1i6S-O5WHNKwJUrnfa3nyg6gYTjxvt9lAmybSaMIxHfmKWsmtdD9vsTc8hfJnpbDkCzefH884yLKEneNh_VHwzwBNktPyCfzkP1QWD1P8Po8X5ie26jt7P4XE4JWm2n55iNxYwmvgQiyn60Rmyd6uOLTkZOPg6aeb98gqktgQfb2njSLCcWjUPG1ZeLL7-oYanfSBiCGlqPC-ebmxaEqxXq6wHVQAMrF4fRe6gFiHLney_a5sVJHw_I80Hs_QChh9jyu32Sn-pjOq0MbA1KTGoIbHlkf08wkSvi-Ub9zmjqYsbhadx7QRbdSSFq2_k_";

        BasicSessionCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
        // 2 设置 bucket 的地域
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分
        Region region = new Region("ap-beijing"); //COS_REGION 参数：配置成存储桶 bucket 的实际地域，例如 ap-beijing，更多 COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端
        COSClient cosClient = new COSClient(cred, clientConfig);

        // 指定要上传的文件
        File localFile = new File("C:\\Users\\QUKAN\\Pictures\\Saved Pictures\\222.jpeg");
        // 指定文件将要存放的存储桶
//        String bucketName = "q4-record-1300314960";
        String bucketName = "qukan-test-1300314960";

        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = "folder/test2.jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        System.out.println("==============================================================3");
    }


}

